package com.jurassic.demo.design;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
 * @Author pan_jin
 * @Description: $
 * @Date: 2022/7/28 13:17
 * @Version: 1.0.0
 * @Slogan: 梦想还是要有的万一实现了呢
 */
public class EnumUtil {
    private static Map<Class, Object> map = new ConcurrentHashMap<>();

    public static <T> Optional<T> getEnumObject(Class<T> className, Predicate<T> predicate) {
        if (!className.isEnum()) {
            return null;
        }
        Object o = map.get(className);
        T[] t = null;
        if (o == null) {
            t = className.getEnumConstants();
            map.put(className, t);

        } else {
            t = (T[]) o;
        }
        return Arrays.stream(t).filter(predicate).findAny();
    }
}

